package com.example.yoho.mvp.view.fragment;





import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.EncryptUtils;
import com.example.lib_core.mvp.view.BaseFragment;
import com.example.yoho.R;
import com.example.yoho.di.component.DaggerLoginComponent;
import com.example.yoho.di.moudles.LoginMoudles;
import com.example.yoho.domain.Domain_Url;
import com.example.yoho.mvp.contract.LoginContract;
import com.example.yoho.mvp.model.entity.LoginEntity;
import com.example.yoho.mvp.presenter.LoginPresenter;
import com.example.yoho.mvp.view.activity.UserActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragment<LoginPresenter> implements LoginContract.View, View.OnClickListener {


    private android.widget.ImageView imClear;
    private android.widget.TextView tvRegister;
    private android.widget.EditText etUsername;
    private android.widget.EditText etPassword;
    private android.widget.Button btLogin;

    @Override
    public int bondLayout() {

        return R.layout.fragment_login;
    }

    @Override
    public void Inject() {
        DaggerLoginComponent.builder().loginMoudles(new LoginMoudles(this)).build().Inject(this);
    }

    @Override
    public void initView() {

        imClear = (ImageView) findviewByIds(R.id.im_clear);
        imClear.setOnClickListener(this);
        tvRegister = (TextView) findviewByIds(R.id.tv_register);
        tvRegister.setOnClickListener(this);
        etUsername = (EditText) findviewByIds(R.id.et_username);
        etPassword = (EditText) findviewByIds(R.id.et_password);
        btLogin = (Button) findviewByIds(R.id.bt_login);
        btLogin.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void showLogin(LoginEntity loginEntity) {
        if (loginEntity.getStatues().equals("1")){
            Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
            HashMap<String, Object> map = new HashMap<>();
            map.put("aaa","岳舒凯真帅");
            EventBus.getDefault().post(map);
            getActivity().finish();

        }else {
            Toast.makeText(getContext(), "登录失败", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.im_clear:
                getActivity().finish();
                break;
            case R.id.tv_register:
                UserActivity activity = (UserActivity) getActivity();
                activity.ViewPages(1);
                break;
            case R.id.bt_login:
                String name = etUsername.getText().toString();
                String pwd = etPassword.getText().toString();

                byte[] buf = EncryptUtils.encryptRSA(pwd.getBytes(),
                        android.util.Base64.decode(Domain_Url.privateKey.getBytes(), android.util.Base64.DEFAULT)
                        ,false,"RSA/ECB/PKCS1Padding");
                byte[]buff = android.util.Base64.encode(buf, android.util.Base64.DEFAULT);
                String rasPwd = new String(buff);
                Log.e("yskpwd", "onClick: "+rasPwd );

                mPresenter.loginAll(name,rasPwd);
                break;
        }
    }
}
